<?php
class PreferencesController extends Zend_Controller_Action
{

    public function init () {
        $init = new Model_Init($this);
    }

    public function indexAction () {}

    public function sexAction () {
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $user = new Model_User($id);
        $form = new Form_SexEdit();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                $user->fill($form->getValues());
                $user->save();
                
                $this->updateLucence($user);
                
                $this->view->message = $this->view->translate(
                "Gender has been changed");
            }
        } else {
            $form->populate($user->populateForm());
        }
        $this->view->form = $form;
    }

    public function passwordAction () {
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $user = new Model_User($id);
        $form = new Form_PasswordEdit();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                $user->fill($form->getValues());
                $pass_not_encrypted = $user->password;
                //encrypte password
                $user->password = md5($user->password);
                $user->save();
                $mail_sender = new Model_PasswordMail();
                $mail_sender->sendMailNewPass($user->email, $pass_not_encrypted, 
                $this);
                $this->view->message = $this->view->translate(
                "Password has been changed");
            }
        } else {
            $form->populate($user->populateForm());
        }
        $this->view->form = $form;
    }

    //TODO change data/avatars folder permissions
    public function avatarAction () {
        $request = $this->getRequest();
        $form = new Form_AvatarEdit();
        
        //save pic url for user
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $user = new Model_User($id);
        
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                $file = $form->avatar->getFileInfo();
                
                $file_name = pathinfo($form->avatar->getFileName());
                $newFilename = 'file-' . uniqid() . '.' . $file_name['extension'];
                
                $ready_file_path = realpath(dirname('.')) . DIRECTORY_SEPARATOR .
                 'upload' . DIRECTORY_SEPARATOR;
                
                $form->avatar->addFilter('Rename', 
                $ready_file_path . $newFilename);
                $form->avatar->receive();
                
                //resize image to 150 px
                $thumb = Gd_PhpThumbFactory::create(
                $ready_file_path . $newFilename);
                $thumb->resize(150, 150)->save($ready_file_path . $newFilename);
                
                $ava_thumb =  $ready_file_path . 'thumb' . DIRECTORY_SEPARATOR . $newFilename;
                //create thumbnail for image
                $thumb->resize(120, 120)->save($ava_thumb);
                
                $user->fill($form->getValues());
                $user->save();
                $this->view->message = $this->view->translate(
                "Logo has been changed");
                
                //add action that user add new avatar and then show it thumb in news line
                $model_action = new Model_Action();
                $model_action->addAction($id, 1, $newFilename);
            }
        }
        $this->view->form = $form;
    }

    public function removeavatarAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $user = new Model_User($id);
        $user->avatar = '';
        $user->save();
        $this->_helper->redirector('', '');
    }

    public function nulliconsAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $id = Zend_Auth::getInstance()->getIdentity()->id;
        $user = new Model_User($id);
        $user->ava_x = 0;
        $user->ava_y = 0;
        
        $user->sex_x = 0;
        $user->sex_y = 0;
        
        $user->status_x = 0;
        $user->status_y = 0;
        
        $user->album_x = 0;
        $user->album_y = 0;
        
        $user->save();
        $this->_helper->redirector('', '');
    }

    public function cleanvisitAction () {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $model_guest = new Model_Guest();
        $user_id = Zend_Auth::getInstance()->getIdentity()->id;
        
        $model_guest->setAllVisited($user_id);
        $this->_helper->redirector('', '');
    }

    public function statusAction () {
        $form = new Form_Status();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()
                ->getPost())) {
                $id = Zend_Auth::getInstance()->getIdentity()->id;
                $user = new Model_User($id);
                $user->fill($form->getValues());
                $user->save();
                
                $this->updateLucence($user);
                
                $this->view->message = $this->view->translate(
                'Status has been changed');
                
                 //add action that user add new photo to album
                $model_action = new Model_Action();
                $model_action->addAction($id, 4, $form->getValue('status'));
            }
        }
        $this->view->form = $form;
    }

    /**
     * @param user
     */
    private function updateLucence ($user) {
        //add login, name, surname to SearchLucence
        $user_item = new Model_User($user->getLastInsertedId());
        
        $model = new Model_Search('users');
        $model->update_item($user_item->id, $user_item->name, 
        $user_item->surname, $user_item->login);
    }

}


